import axios from "axios";
import { Modal } from "antd";
const context = import.meta.env.VITE_SERVER_URL;
console.log("context:",context)
// 创建一个基于基本配置的axios实例
const http = axios.create({
  baseURL: context,
  timeout: 30000, // 大于30秒超时
  headers: {
    "Content-Type": "application/json;charset=UTF-8",
  },
});

// 添加请求拦截器
http.interceptors.request.use((config) => {
  console.log("发送请求");
//   Modal.warning({
//     content: "发送请求拦截",
//     centered: true,
//     okText: "确认",
//     onOk() {
//       // 弹出登录弹框
//     },
//   });
  return config;
});
// 添加响应拦截器
http.interceptors.response.use(
  (response) => {
    console.log("接收响应");

    console.log(response)

    if(response.data.code !== '0'){
       Modal.error({
        title:"ERROR",
        content: response.data.msg,
        centered: true,
        okText: "确认",
        onOk() {
          // 弹出登录弹框
        },
      });
    }else{
      return response.data;
    }
    
  },
  (error) => {
    console.log("TCL: error", error);
    const msg = error.Message !== undefined ? error.Message : "";
    Modal.warning({
        content: "发生错误",
        centered: true,
        okText: "确认",
        onOk() {
          // 弹出登录弹框
        },
      });
    return Promise.reject(error);
  }
);

export default http;